package com.linpq.road.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.linpq.common.annotation.Log;
import com.linpq.common.enums.BusinessType;
import com.linpq.road.domain.RdManagementUnit;
import com.linpq.road.service.RdManagementUnitService;
import com.linpq.common.core.controller.BaseController;
import com.linpq.common.core.domain.AjaxResult;
import com.linpq.common.utils.poi.ExcelUtil;
import com.linpq.common.core.page.TableDataInfo;

/**
 * 管养单位Controller
 *
 * @author linpq
 * @date 2021-07-28
 */
@Controller
@RequestMapping("/road/management_unit")
public class RdManagementUnitController extends BaseController {
    private String prefix = "road/management_unit";

    @Autowired
    private RdManagementUnitService rdManagementUnitService;

    @RequiresPermissions("road:management_unit:view")
    @GetMapping()
    public String management_unit() {
        return prefix + "/management_unit";
    }

    /**
     * 查询管养单位列表
     */
    @RequiresPermissions("road:management_unit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RdManagementUnit rdManagementUnit) {
        startPage();
        List<RdManagementUnit> list = rdManagementUnitService.selectRdManagementUnitList(rdManagementUnit);
        return getDataTable(list);
    }

    /**
     * 导出管养单位列表
     */
    @RequiresPermissions("road:management_unit:export")
    @Log(title = "管养单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RdManagementUnit rdManagementUnit) {
        List<RdManagementUnit> list = rdManagementUnitService.selectRdManagementUnitList(rdManagementUnit);
        ExcelUtil<RdManagementUnit> util = new ExcelUtil<RdManagementUnit>(RdManagementUnit. class);
        return util.exportExcel(list, "management_unit");
    }

    /**
     * 新增管养单位
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存管养单位
     */
    @RequiresPermissions("road:management_unit:add")
    @Log(title = "管养单位", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RdManagementUnit rdManagementUnit) {
        return toAjax(rdManagementUnitService.insertRdManagementUnit(rdManagementUnit));
    }

    /**
     * 修改管养单位
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        RdManagementUnit rdManagementUnit =rdManagementUnitService.selectRdManagementUnitById(id);
        mmap.put("rdManagementUnit", rdManagementUnit);
        return prefix + "/edit";
    }

    /**
     * 修改保存管养单位
     */
    @RequiresPermissions("road:management_unit:edit")
    @Log(title = "管养单位", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RdManagementUnit rdManagementUnit) {
        return toAjax(rdManagementUnitService.updateRdManagementUnit(rdManagementUnit));
    }

    /**
     * 删除管养单位
     */
    @RequiresPermissions("road:management_unit:remove")
    @Log(title = "管养单位", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(rdManagementUnitService.deleteRdManagementUnitByIds(ids));
    }
}
